﻿// This source is subject to Microsoft Public License (Ms-PL).
// Please see http://taskcardcreator.codeplex.com for details.
// All other rights reserved.

using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using Microsoft.TeamFoundation.WorkItemTracking.Client;

namespace WpfControlLibrary
{
  public delegate void SelectionChangeEventHandler(string queryString);

  /// <summary>
  /// Interaction logic for UserControl1.xaml
  /// </summary>
  public partial class WorkItemsQueriesControl : UserControl
  {
    public event SelectionChangeEventHandler SelectionChanged;

    private enum QueryTypes
    {
      Folder,
      MyQ,
      TeamQ,
      FView,
      DView,
      HView,
      None
    }

    private string projectName;

    public WorkItemsQueriesControl()
    {
      InitializeComponent();
    }

    public void BuildQueryTree(QueryHierarchy queryHierarchy, string project)
    {
      projectName = project;
      BuildQueryHierarchy(queryHierarchy, project);
    }

    private void BuildQueryHierarchy(QueryHierarchy QueryHierarchy, string project)
    {
      TreeViewItem root = new TreeViewItem {IsExpanded = true, Header = project};

      foreach (QueryFolder query in QueryHierarchy)
      {
        DefineFolder(query, root);
      }

      Queries.Items.Add(root);
    }

    private void DefineFolder(QueryFolder query, TreeViewItem father)
    {
      TreeViewItem item = new TreeViewItem {IsExpanded = true};
      QueryTypes type = QueryTypes.Folder;

      if (query.IsPersonal) type = QueryTypes.MyQ;
      else if (query.Name == "Team Queries") type = QueryTypes.TeamQ;

      item.Header = CreateTreeItem(query.Name, type);

      father.Items.Add(item);

      foreach (QueryItem sub_query in query)
      {
        if (sub_query.GetType() == typeof(QueryFolder))
          DefineFolder((QueryFolder)sub_query, item);
        else
          DefineQuery((QueryDefinition)sub_query, item);
      }
    }

    private static StackPanel CreateTreeItem(string value, QueryTypes type)
    {
      StackPanel stake = new StackPanel {Orientation = Orientation.Horizontal};

      Image img = new Image
                    {
                      Stretch = System.Windows.Media.Stretch.Uniform,
                      Source = GetImage(type),
                      Margin = new Thickness(2, 0, 5, 0)
                    };
      Label lbl = new Label {Content = value};

      stake.Children.Add(img);
      stake.Children.Add(lbl);

      return stake;
    }

    private void DefineQuery(QueryDefinition query, TreeViewItem QueryFolder)
    {
      TreeViewItem item = new TreeViewItem {IsExpanded = true};
      QueryTypes type;

      switch (query.QueryType)
      {
        case QueryType.List: type = QueryTypes.FView; break;
        case QueryType.OneHop: type = QueryTypes.DView; break;
        case QueryType.Tree: type = QueryTypes.HView; break;
        default: type = QueryTypes.None; break;
      }

      item.Header = CreateTreeItem(query.Name, type);
      item.Tag = query;
      item.MouseUp += item_MouseUp;
      QueryFolder.Items.Add(item);
    }

    private void item_MouseUp(object sender, MouseButtonEventArgs e)
    {
      TreeViewItem item = (TreeViewItem)sender;
      var query = ((QueryDefinition)item.Tag).QueryText;
      query = query.Replace("@project", "'" + projectName + "'");

      if (SelectionChanged != null)
        SelectionChanged(query);
    }

    static BitmapSource GetImage(QueryTypes type)
    {
      switch (type)
      {
        case QueryTypes.MyQ:
          return DisplayImage.GetImageSource(Properties.Resources.MyQ);
        case QueryTypes.TeamQ:
          return DisplayImage.GetImageSource(Properties.Resources.TeamQ);
        case QueryTypes.Folder:
          return DisplayImage.GetImageSource(Properties.Resources.Folder);
        case QueryTypes.FView:
          return DisplayImage.GetImageSource(Properties.Resources.FView);
        case QueryTypes.DView:
          return DisplayImage.GetImageSource(Properties.Resources.DView);
        case QueryTypes.HView:
          return DisplayImage.GetImageSource(Properties.Resources.HView);
        default:
          return null;
      }
    }
  }
}
